import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
// import componentsRouter from './modules/components'
// import chartsRouter from './modules/charts'
// import tableRouter from './modules/table'
// import nestedRouter from './modules/nested'

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  // 注册
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  // 默认路由
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    // hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        // meta: { title: 'Profile', icon: 'user', noCache: true }
        // meta: { title: '个人中心', icon: 'user', noCache: true },
        meta: {
          title: '个人中心',
          icon: 'user',
          roles: ['patient', 'doctor'],
          noCache: true }
      }
    ]
  },
  // 医生工作站
  {
    path: '/doctorWorkstation',
    component: Layout,
    redirect: '/doctorWorkstation/medicalEdit',
    name: 'doctorWorkstation',
    meta: {
      title: '医生工作站',
      icon: 'el-icon-s-flag',
      roles: ['doctor']
    },
    children: [
      { // 病历编辑
        path: 'medicalEdit',
        component: () => import('@/views/docter/medicalEdit.vue'),
        name: 'medicalEdit',
        meta: { title: '病历编辑', icon: 'el-icon-edit-outline' }
      },
      { // 检验申请
        path: 'checkApply',
        component: () => import('@/views/docter/checkApply.vue'),
        name: 'checkApply',
        meta: { title: '检验申请', icon: 'el-icon-coordinate' }
      },
      { // 处方开具
        path: 'prescription',
        component: () => import('@/views/docter/prescription.vue'),
        name: 'prescription',
        meta: { title: '处方开具', icon: 'el-icon-toilet-paper' }
      },
      {
        // 录入检查结果
        path: 'checkResult',
        component: () => import('@/views/docter/checkResult.vue'),
        name: 'checkResult',
        meta: { title: '录入检查结果', icon: 'el-icon-s-claim' }
      }
    ]
  },
  // 预约挂号
  {
    path: '/reserve',
    component: Layout,
    redirect: '/reserve/reservePage',
    alwaysShow: true, // will always show the root menu
    name: 'reserve',
    meta: {
      title: '预约挂号',
      icon: 'people',
      roles: ['patient'] // you can set roles in root nav
    },
    children: [
      {
        path: 'reservePage',
        component: () => import('@/views/reserve/reservePage'),
        name: 'reservePage',
        meta: {
          title: '预约挂号'
        }
      },
      {
        path: 'myReserve',
        component: () => import('@/views/reserve/myReserve'),
        name: 'myReserve',
        meta: {
          title: '我的挂号'
        }
      }
    ]
  },
  // 划价收费
  {
    path: '/price',
    component: Layout,
    redirect: '/price/pricePage',
    children: [
      {
        path: 'pricePage',
        component: () => import('@/views/price/pricePage'),
        name: 'pricePage',
        meta: {
          title: '划价收费',
          icon: 'shopping',
          roles: ['patient'] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/excel',
    component: Layout,
    redirect: '/excel/export-selected-excel',
    name: 'excel',
    meta: {
      title: '药房',
      icon: 'excel',
      roles: ['medicine']
    },
    children: [
      {
        path: 'export-selected-excel',
        component: () => import('@/views/excel/select-excel'),
        name: 'SelectExcel',
        meta: { title: '药品展示' }
      },
      {
        path: 'upload-excel',
        component: () => import('@/views/excel/upload-excel'),
        name: 'UploadExcel',
        meta: { title: '药品管理' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

// 创建路由对象，内容即constantRoutes在上面
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
